package com.yozinga.facebook;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yozinga.R;

public class AndroidFacebookSample extends Activity {

	private static final String FACEBOOK_APPID = "610748678957524";
	private static final String FACEBOOK_PERMISSION = "publish_stream";
	private static final String TAG = "FacebookSample";

	private final Handler mFacebookHandler = new Handler();
	private TextView loginStatus;
	private FacebookConnector facebookConnector;
	String coupontitle,friendlyurl,couponText,imageNumber;
	/* final Runnable mUpdateFacebookNotification = new Runnable() {
        public void run() {
        	Toast.makeText(getApplicationContext(), "Facebook updated !", Toast.LENGTH_LONG).show();
        }
    };*/

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facebooklogin);

		this.facebookConnector = new FacebookConnector(FACEBOOK_APPID, this, getApplicationContext(), new String[] {FACEBOOK_PERMISSION});

		coupontitle = "Hai Friends GoodNight";
		friendlyurl = "Test Message for Facebook integration from Android SDK";


		loginStatus = (TextView)findViewById(R.id.login_status);
		loginStatus.setVisibility(View.GONE);

		Button tweet = (Button) findViewById(R.id.btn_post);
		// Button clearCredentials = (Button) findViewById(R.id.btn_clear_credentials);



		tweet.setOnClickListener(new View.OnClickListener() {
			/**
			 * Send a tweet. If the user hasn't authenticated to Tweeter yet, he'll be redirected via a browser
			 * to the twitter login page. Once the user authenticated, he'll authorize the Android application to send
			 * tweets on the users behalf.
			 */
			public void onClick(View v) {
				postMessage();
			}
		});

		/* clearCredentials.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
            	clearCredentials();
            	 Toast.makeText(getApplicationContext(), "Logged out succesfully", Toast.LENGTH_SHORT).show();
            	updateLoginStatus();
            }
        });*/
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
	}


	/*@Override
	protected void onResume() {
		super.onResume();
		updateLoginStatus();
	}

	public void updateLoginStatus() {
		loginStatus.setText("Logged into Twitter : " + facebookConnector.getFacebook().isSessionValid());
	}*/



	public void postMessage() {

		if (facebookConnector.getFacebook().isSessionValid()) {
			postMessageInThread();
		} else {
			SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {

				public void onAuthSucceed() {
					postMessageInThread();
				}


				public void onAuthFail(String error) {

				}
			};
			SessionEvents.addAuthListener(listener);
			facebookConnector.login();
		}
	}

	private void postMessageInThread() {
		Thread t = new Thread() {
			public void run() {

				try {
					facebookConnector.postMessageOnWall(getFacebookMsg());
					mFacebookHandler.post(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(getApplicationContext(), "Facebook updated !", Toast.LENGTH_LONG).show();	
						}
					});
				} catch (Exception ex) {
					Log.e(TAG, "Error sending msg",ex);
				}
			}
		};
		t.start();
	}

	private String getFacebookMsg() {
		return  coupontitle+"\n"+friendlyurl;
	}	

	/*private void clearCredentials() {
		try {
			facebookConnector.getFacebook().logout(getApplicationContext());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}*/
}